#! /bin/bash

# hostname=$1

#1.ip地址修改，目前只能每台机器独自修改ip地址
# echo "1.ip地址修改暂无"

#2.修改机器名hostname
# echo "2.修改hostname为node1"
# /home/hadoop/automaticDeploy/systems/changeHostname.sh $hostname

set_centos_repo_to_aliyun() {
    # 定义yum源的路径
    local repo_file="/etc/yum.repos.d/CentOS-Base.repo"

    # 备份原有的yum源
    cp -a "${repo_file}" "${repo_file}.bak"

    # 写入新的yum源
    echo -e "# CentOS-Base.repo
[base]
name=CentOS-\$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-\$releasever - Updates
baseurl=http://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras
baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of CentOS like development tools and software
[centosplus]
name=CentOS-\$releasever - Plus
baseurl=http://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-\$releasever - Contrib
baseurl=http://mirrors.aliyun.com/centos/\$releasever/contrib/\$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7" > "${repo_file}"

    # 清理yum缓存
    yum clean all

    # 重新生成缓存
    yum makecache
}

# 检查yum能否安装所需依赖
install_dependency() {
    local pkg_name=$1
    local pkg_command=$2

    echo "配置阿里云镜像源"
    # 配置阿里云镜像源
    set_centos_repo_to_aliyun

    # 检查依赖项是否已安装
    if ! command -v $pkg_name &> /dev/null; then
        echo "$pkg_name 未安装"
        if command -v yum &> /dev/null; then
            echo "尝试使用 yum 安装 $pkg_name..."
            yum install -y $pkg_command
            if [ $? -eq 0 ]; then
                echo "$pkg_name 安装成功"
            else
                echo "无法使用 yum 安装 $pkg_name，请确保可以访问外网或配置本地yum源"
                exit 1
            fi
        else
            echo "无法使用 yum 安装 $pkg_name，请确保可以访问外网或配置本地yum源"
            exit 1
        fi
    else
        echo "$pkg_name 已安装"
    fi
}

check_dependency() {
    install_dependency "expect" "expect"
    install_dependency "ntpdate" "ntpdate"
}

# 获取环境变量
PLAY_HOME=/opt/playground

jdk_bool=$1

# 将环境变量持久化到/etc/profile中
profile=/etc/profile
sed -i "/^export PLAY_HOME/d" $profile
echo "export PLAY_HOME=$PLAY_HOME" >> $profile

# 更新/etc/profile文件
source /etc/profile && source /etc/profile

# 配置DNS
echo "DNS配置"
$PLAY_HOME/systems/configDNS.sh

# 检查并安装所需依赖
check_dependency

# NTP时间同步
yum install ntpdate -y
ntpdate cn.pool.ntp.org 

# host配置文件修改
echo "将集群ip及其映射的hostname添加到/etc/hosts中"
$PLAY_HOME/systems/addClusterIps.sh

# 关闭防火墙、SELINUX ，需要输入参数close或start
echo "关闭防火墙、SELINUX"
$PLAY_HOME/systems/closeFirewall.sh close

# 添加bigdata用户名 ，需要输入参数create或delete
# echo "添加bigdata用户名"
# /home/hadoop/automaticDeploy/systems/autoCreateUser.sh create

# 配置yum源
# echo "配置yum源"
# /home/hadoop/automaticDeploy/systems/configureYum.sh $hostname

# 配置SSH无密码登录
echo "集群各节点之间配置SSH无密码登录"
$PLAY_HOME/systems/sshFreeLogin.sh "do"

# 检查免密登录是否生效
$PLAY_HOME/systems/sshFreeLogin.sh "check"

# 配置JDK环境
if [ "$jdk_bool" = "1" ]; then
    echo "配置 JDK 环境..."
    $PLAY_HOME/systems/configureJDK.sh
else
    echo "跳过 JDK 安装"
    # 在这里可以继续执行其他操作
fi

#9.配置SCALA环境
# echo "9.配置scala环境"
# /home/hadoop/automaticDeploy/systems/configureScala.sh
echo "--------------------"
echo "|   环境初始化成功！|"
echo "--------------------"